package com.bwie.unit;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCAJ9x6cQyY4EQxPZUBdKf/EdghCVI5Qtw8VXv5EMX0Qmje/Bc2lO8QD0P/mjfGZX+FwgdRvrPJRAPdXphcCHhlLHF3Ew2J3z/UDq8Kp2Knaj+yxFy93HVSjwESJ7ywC6OXHv2x0R3JO9WTdclXRUSvtp71yiY5ppga/fbS46yvjsQuuDQwjM19jt4ux1NrhSg9Cvv7p2+kV7fViLX/rY5r+TxFlMvYZSKmOaDHg9ZjOFIwph37oZBsY0ljY7bCh0wB08vq+GIKpy6vna69ty9/9oeMhpfbb4zJLORZtkqsg13tp4Xz5/MVbZLBzAUl2rHSi19p+PEyMVQxSGQ7SUepAgMBAAECggEAYKOucvaE1VNomOkjT/G0LXg7Ts5EnsF4albcIbkwTT0278+RgatVivq4ACs/G/C19RuXgWEZGhCcCXan1d7dSw30kiJf9zPOYg6gmJwYnL5WQmwos1NndVY5LIOime5s8iyAinJyz6i7KXOM3K5zWT2RKS/DG34w/Um9stHVCpzAkarDmrU35HgdhLA+130hHLyry16AlTk6YwF23eoeRZ7VorK+vakH/qEiTsSqpfX5+/pjMKqA7fgGDfQCLGkv5C1LIfmHW/mGfZ8TxaCKy/JwohYLDIEjV0NajPU9cqALZFNIt9rMcAm80p0WKVIWSGtnjm1aWKzH2vQEkH/D8QKBgQC829f2k0lh+/718LR1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ykPHw23ZIlc3EQltCF/+8BtRsF8QBBy2W74t1EDdDG0f/j02yb49CLnsm3fDTe+daVLta/OSvyZ7KqiLiA4XSqs5y+jZqUnaIW/BN4zlG4AnIDJMfOA1v53/lY04mGvavk3WFdsL/fPcqgAG430o4ZWASg2qZO61bHg4q504mCeOAaHFjPldQIDAQAB";
    //生成支付订单接口
    public String pay(String id,String jiege) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141651495");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl("http://localhost:8084/journey/huidiao");
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(id);
        model.setTotalAmount(jiege);
        model.setSubject("滴滴打车");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        return response.getBody();
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141651495");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
